<?php
namespace app\index\controller;

use think\Session;
use app\common\model\Imgcategory as ImgcategoryModel;
use app\common\model\Images as ImagesModel;
use app\common\model\User as UserModel;
use app\api\controller\Uploads;
use app\common\controller\HomeBase;

class User extends HomeBase
{
    protected $imgcategory_model;
    protected $images_model;
    protected $user_model;
    protected $index_id;

    protected function _initialize(){
        parent::_initialize();
        if(!session::get('index_id')){
            $this->redirect('index/index/index');
        }
        $this->index_id = session::get('index_id');
        $this->imgcategory_model = new ImgcategoryModel();
        $this->images_model = new ImagesModel;
        $this->user_model = new UserModel;
        $category = $this->imgcategory_model->where('pid',75)->select();
        $this->assign('category',$category);
    }

    public function user($page = 1){
        if(!session::get('index_id')){
            $this->redirect('index/index/index');
        }
        $index_id = session::get('index_id');
        $map = [
            'user_id' => ['eq',$index_id],
            'status' => ['eq',1],
        ];
        $id = 75;
        $sid = $this->imgcategory_model->where('pid',$id)->column('id');
        $img_list = $this->images_model->where($map)->whereIn('pid',$sid)->paginate(20,false,['page'=>$page]);
        $this->assign('img_list',$img_list);
        $user_info = $this->user_model->where('id',$index_id)->find();
        $this->assign('user_info',$user_info);

        return $this->fetch();
    }

    public function userbk($page = 1){
        if(!session::get('index_id')){
            $this->redirect('index/index/index');
        }
        $index_id = session::get('index_id');
        $map = [
            'user_id' => ['eq',$index_id],
            'status' => ['eq',1],
        ];

        $order = 'id DESC,readding DESC,create_time DESC,downloadding DESC';
        $id = 75;
        $sid = $this->imgcategory_model->where('pid',$id)->column('id');
        $img_list = $this->images_model->where($map)->whereIn('pid',$sid)->order($order)->paginate(20,false,['page'=>$page]);
        $user_info = $this->user_model->alias('u')->field("u.*,(SELECT count(*) FROM think_images WHERE user_id = {$index_id}) i_count,(SELECT count(*) FROM think_wenan WHERE user_id = {$index_id}) w_count")->where('u.id',$index_id)->find();
        $this->assign('user_info',$user_info);
        $this->assign('img_list',$img_list);
        return $this->fetch();
    }

    public function update(){
        if($this->request->isPost()){
            $index_id = session::get('index_id');
            $data = $this->request->post();
            $result = $this->user_model->allowField(true)->save($data,['id' => $index_id]);
            if($result){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
    }

    public function setBgImage(){
        $file = $this->request->file('bg_file');
        if($file){
            $upload = new Uploads();
            $path = $upload->upload($file);
            if($path['error'] == 0){
                $result = $this->user_model->allowField(true)->save(['bg_img'=>$path['url']],['id' => $this->index_id]);
                if($result){
                    return json(['status'=> 1 , 'msg' => 'ok']);
                }else{
                    return json(['status' => -1 , 'error' => 'error']);
                }
            }else{
                return json(['status' => -1 , 'error' => 'upload failed']);
            }
        }
    }

    public function setHeaderImg(){
        $file = $this->request->file('headImg');
        if($file){
            $upload = new Uploads();
            $path = $upload->upload($file);
            if($path['error'] == 0){
                $result = $this->user_model->allowField(true)->save(['headImg'=>$path['url']],['id' => $this->index_id]);
                if($result){
                    return json(['status'=> 1 , 'msg' => 'ok']);
                }else{
                    return json(['status' => -1 , 'error' => 'error']);
                }
            }else{
                return json(['status' => -1 , 'error' => 'upload failed']);
            }
        }
    }

    public function wenan(){
        return $this->fetch();
    }
}